//
//  logInView.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/15.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "logInView.h"
#import "regAndForgPaswdView.h"
#import "homePageVC.h"
#import "BaseNavigationConler.h"
@implementation logInView


- (void)awakeFromNib {
    [super awakeFromNib];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"token"] != nil && ![[[NSUserDefaults standardUserDefaults] objectForKey:@"token"] isEqualToString:@""]) {
        //被挤掉时可以使用
        self.phoneNumber_tf.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
        self.passWord_tf.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
    }
}
- (void)setHIDDEN:(NSString *)HIDDEN {
    _HIDDEN = HIDDEN;
    if ([self.HIDDEN isEqualToString:@"NO"]) {
        self.fanhui_btn.hidden = NO;
    }else {
        self.fanhui_btn.hidden = YES;
    }
}
- (IBAction)returnClick_btn:(id)sender {
    [[helpPublic getCurrentViewController:self] dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)memoryPassWord_btn:(id)sender {
    
}
- (IBAction)forgetPasswordClick_btn:(id)sender {
    [self pushToViewWithType:@"2"];
}
- (IBAction)immediatelyLogin_btn:(id)sender {
    [self logInRequest];
}
- (IBAction)nowRegister_btn:(id)sender {
    [self pushToViewWithType:@"1"];
}
- (void)pushToViewWithType:(NSString *)type {
    regAndForgPaswdView *subView = [[[NSBundle mainBundle] loadNibNamed:@"regAndForgPaswdView" owner:self options:nil] lastObject];
    subView.frame = self.frame;
    subView.type = type;
    [UIView transitionWithView:self duration:1 options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        [self addSubview:subView];
                    } completion:^(BOOL finish){
                        
                    }];
}
#pragma mark --- 登录请求
- (void)logInRequest {
    if ([self.phoneNumber_tf.text isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入手机号码"];
        return;
    }else if ([self.passWord_tf.text isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入密码"];
        return;
    }
    NSString *result = [self.passWord_tf.text transactionPasswordReguler];
    if (![result isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:result];
        return;
    }
    NSDictionary *dic = @{
                          @"username" : self.phoneNumber_tf.text,
                          @"password" : self.passWord_tf.text
                          };
    [networkManger PostHttpDataWithUrlStr:[[apiManger shareApiManger] lonIn] Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user removeObjectForKey:@"token"];
            [user removeObjectForKey:@"userName"];
            [user removeObjectForKey:@"password"];
            [user setObject:responseObject[@"token"] forKey:@"token"];
            [user setObject:responseObject[@"username"] forKey:@"userName"];
            [user setObject:self.passWord_tf.text forKey:@"password"];
            [user synchronize];
            [self pushRootVC];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)pushRootVC {
    BaseNavigationConler *RootVC = (BaseNavigationConler *)self.window.rootViewController;
    UIViewController *VC3 = RootVC.viewControllers.firstObject;
    UIViewController *VC2 = [helpPublic getCurrentViewController:self];
    if ([VC3 isKindOfClass:[VC2 class]]) {
        BaseNavigationConler * navc = [[BaseNavigationConler alloc] initWithRootViewController:[homePageVC new]];
        self.window.rootViewController = navc;
    }else {
        [VC2 dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)dealloc {
}
@end
